﻿using Asxsyd92Core.Utils;
using Asxsyd92Core.Utils.Provider;
using System;
using System.Collections.Generic;
using System.Linq;
using WebOS.Dal;
using WebOS.Modle;

namespace WebOS.Bll
{
    public class OrganizeBll
    {
        public static OrganizeBll Instance
        {
            get { return SingletonProvider<OrganizeBll>.Instance; }
        }



        /// <summary>
        /// 得到一组机构字符串下所有人员
        /// </summary>
        /// <param name="idString"></param>
        /// <returns></returns>
        public List<Users> GetAllUsers(string idString)
        {
            if (string.IsNullOrEmpty(idString))
            {
                return new List<Users>();
            }
            string[] idArray = idString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<Users> userList = new List<Users>();
            WorkGroup bwg = new WorkGroup();
            foreach (string id in idArray)
            {
                if (id.StartsWith("u_"))//人员
                {
                    userList.Add(Dal.UsersDal.Instance.Get(id.Replace("u_", "").ToString().ToGuid()));
                    //userList.Add(busers.Get(Users.RemovePrefix(id).ToGuid()));
                }
                else if (id.IsGuid())//机构
                {
                    userList.AddRange(GetAllUsers(id.ToGuid()));
                }
                else if (id.StartsWith("w_"))//工作组
                {
                    //addWorkGroupUsers(userList, bwg.Get(id.Replace("u_", "").ToString().ToGuid()));
                }
            }
            userList.RemoveAll(p => p == null);
            return userList.ToList();
        }

        /// <summary>
        /// 得到一个机构下的所有人员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Users> GetAllUsers(Guid id)
        {
            var childs = GetAllChilds(id);
            List<string> ids = new List<string>();
            ids.Add(id.ToString());
            foreach (var child in childs)
            {
                ids.Add(child.ID.ToString());
            }
            string formData = "";

            foreach (string url in ids)
            {
                formData += "'" + url + "'" + ",";
            }
            formData = formData.Replace(",", "");
            return UsersDal.Instance.GetList("SELECT u.ID,u.Account,u.Name,o.Name as Organize,o.ID as OrganizeID,o.Leader from  Users u,Organize o,UsersRelation ur where o.ID=ur.OrganizeID and ur.UserID=u.ID and o.Status=0 and ur.OrganizeID in(" + formData + ")", null).ToList();
            //   return UsersDal.Instance.GetList("",null).ToList();//.GetAllByOrganizeIDArray(ids.ToArray());
        }
        /// <summary>
        /// 查询一组岗位下所有人员
        /// </summary>
        /// <param name="organizeID"></param>
        /// <returns></returns>
        public List<Users> GetAllByOrganizeIDArray(Guid[] organizeIDArray)
        {

            return UsersDal.Instance.GetList("SELECT u.ID,u.Account,u.Name,o.Name as Organize,o.ID as OrganizeID,o.Leader from  Users u,Organize o,UsersRelation ur where o.ID=ur.OrganizeID and ur.UserID=u.ID and o.Status=0 ur.OrganizeID in(" + organizeIDArray.ToArray() + ")", null).ToList();

        }

        /// <summary>
        /// 查询一个组织的所有下级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Organize> GetAllChilds(Guid id)
        {
            var org = Dal.OrganizeDal.Instance.Get(id);
            if (org == null)
            {
                return new List<Organize>();
            }
            return Dal.OrganizeDal.Instance.GetWhere(new { Number = org.Number }).ToList();//.GetAllChild(org.Number);
        }
    }
}